<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input clearable @clear="getGoodsList" v-model="queryData.query" placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="goods" border strip>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column width="130" label="商品价格(元)" prop="goods_price"></el-table-column>
        <el-table-column width="130" label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column width="160" label="创建时间" prop="add_time">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column width="160" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button @click="removeGoods(scope.row.goods_id)" type="danger" icon="el-icon-delete" size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryData.pagenum"
      :page-sizes="[5, 10, 20, 40]"
      :page-size="queryData.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goods: [],
      total: 0
    }
  },

  created () {
    this.getGoodsList()
  },

  methods: {
    // 获取商品列表数据
    async getGoodsList () {
      const { data: res } = await this.$axios.get('goods', {
        params: this.queryData
      })
      if (res.meta.status !== 200) return this.$messaga.error('获取数据失败')
      this.goods = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange (pagesize) {
      this.queryData.pagesize = pagesize
      this.getGoodsList()
    },
    handleCurrentChange (pagenum) {
      this.queryData.pagenum = pagenum
      this.getGoodsList()
    },
    // 点击删除
    removeGoods (id) {
      this.$confirm('此操作将永久删除该数据，是否继续？', '友情提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$axios.delete(`goods/${id}`)
        if (res.meta.status !== 200) return this.$message.error('删除成功')
        this.$message.success('删除成功')
        this.getGoodsList()
      }).catch(() => {
        this.$message('已取消删除')
      })
    },
    goAddGoods () {
      this.$router.push('/add/goods')
    }
  }
}
</script>

<style scoped lang='less'>

</style>
